package com.qianfeng.xinyihezi.xinyihezi.adapter;

import android.content.Context;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.qianfeng.xinyihezi.xinyihezi.R;
import com.qianfeng.xinyihezi.xinyihezi.fragment.ClassiftModelFragment;
import com.qianfeng.xinyihezi.xinyihezi.model.DetailSonCategory;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Tcoy on 2016/11/14.
 */

public class RecyclerViewDetailClassiftAdapter extends RecyclerView.Adapter<RecyclerViewDetailClassiftAdapter.ViewHolder> {
    private String TAG = getClass().getSimpleName();
    private List<DetailSonCategory> mListDetail;
    private Context context;
    private LayoutInflater mInflater;

    public RecyclerViewDetailClassiftAdapter(List<DetailSonCategory> mListDetail, Context context) {
        this.mListDetail = mListDetail;
        this.context = context;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewHolder viewHolder = null;
        viewHolder = new ViewHolder(mInflater.inflate(R.layout.classift_detail_grid, null));
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.tvDetailClassiftName.setText(mListDetail.get(position).getName());
        Picasso.with(context).load(mListDetail.get(position).getImage_url()).into(holder.imgDetailClassift);

        holder.imgDetailClassift.setTag(mListDetail.get(position).getCat_id());
        holder.imgDetailClassift.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Message message = Message.obtain();
                String s = String.valueOf(v.getTag());
                int cat_id = Integer.parseInt(s);
                message.arg1 = cat_id;
                ClassiftModelFragment.handler.sendMessage(message);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mListDetail.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView imgDetailClassift;
        private TextView tvDetailClassiftName;

        public ViewHolder(View itemView) {
            super(itemView);
            imgDetailClassift = (ImageView) itemView.findViewById(R.id.imageViewDetailClassiftIcon);
            tvDetailClassiftName = (TextView) itemView.findViewById(R.id.textViewDeailClassiftName);
        }
    }
}
